package com.ruoyi.service.inventory.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.basic.MesMaterial;
import com.ruoyi.domain.inventory.MesInventory;
import com.ruoyi.domain.inventory.MesWarehouse;
import com.ruoyi.mapper.inventory.MesInventoryMapper;
import com.ruoyi.service.basic.IMesMaterialService;
import com.ruoyi.service.inventory.IMesInventoryService;
import com.ruoyi.service.inventory.IMesWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 库存信息Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class MesInventoryServiceImpl implements IMesInventoryService {
    @Autowired
    private MesInventoryMapper mesInventoryMapper;

    @Autowired
    private IMesWarehouseService IMesWarehouseService;

    @Autowired
    private IMesMaterialService IMesMaterialService;

    /**
     * 查询库存信息
     * 
     * @param inventoryId 库存信息主键
     * @return 库存信息
     */
    @Override
    public MesInventory selectMesInventoryByInventoryId(Long inventoryId) {
        return mesInventoryMapper.selectMesInventoryByInventoryId(inventoryId);
    }

    /**
     * 查询库存信息列表
     * 
     * @param mesInventory 库存信息
     * @return 库存信息
     */
    @Override
    public List<MesInventory> selectMesInventoryList(MesInventory mesInventory) {
        return mesInventoryMapper.selectMesInventoryList(mesInventory);
    }

    /**
     * 根据仓库ID和物料ID查询库存
     * 
     * @param warehouseId 仓库ID
     * @param materialId  物料ID
     * @param batchCode   批次号
     * @return 库存信息
     */
    @Override
    public MesInventory selectInventoryByWarehouseAndMaterial(Long warehouseId, Long materialId, String batchCode) {
        return mesInventoryMapper.selectInventoryByWarehouseAndMaterial(warehouseId, materialId, batchCode);
    }

    /**
     * 根据物料编码查询库存列表
     * 
     * @param materialCode 物料编码
     * @return 库存信息集合
     */
    @Override
    public List<MesInventory> selectInventoryByMaterialCode(String materialCode) {
        return mesInventoryMapper.selectInventoryByMaterialCode(materialCode);
    }

    /**
     * 根据仓库编码查询库存列表
     * 
     * @param warehouseCode 仓库编码
     * @return 库存信息集合
     */
    @Override
    public List<MesInventory> selectInventoryByWarehouseCode(String warehouseCode) {
        return mesInventoryMapper.selectInventoryByWarehouseCode(warehouseCode);
    }

    /**
     * 新增库存信息
     * 
     * @param mesInventory 库存信息
     * @return 结果
     */

    @Override
    public int insertMesInventory(MesInventory mesInventory) {
        mesInventory.setCreateTime(new Date());
        mesInventory.setCreateBy(SecurityUtils.getUsername());
        // 根据仓库ID获取仓库信息并设置仓库编码
        if (mesInventory.getWarehouseId() != null &&
                (mesInventory.getWarehouseCode() == null || mesInventory.getWarehouseCode().isEmpty())) {
            MesWarehouse warehouse = IMesWarehouseService.selectMesWarehouseByWarehouseId(mesInventory.getWarehouseId());
            if (warehouse != null) {
                mesInventory.setWarehouseCode(warehouse.getWarehouseCode());
                mesInventory.setWarehouseName(warehouse.getWarehouseName());
            }
        }

        // 根据物料ID获取物料信息并设置物料编码
        if (mesInventory.getMaterialId() != null &&
                (mesInventory.getMaterialCode() == null || mesInventory.getMaterialCode().isEmpty())) {
            MesMaterial material = IMesMaterialService.selectMesMaterialByMaterialId(mesInventory.getMaterialId());
            if (material != null) {
                mesInventory.setMaterialCode(material.getMaterialCode());
                mesInventory.setMaterialName(material.getMaterialName());
                mesInventory.setSpecification(material.getSpecification());
                mesInventory.setUnit(material.getUnit());
            }
        }

        return mesInventoryMapper.insertMesInventory(mesInventory);
    }

    /**
     * 修改库存信息
     * 
     * @param mesInventory 库存信息
     * @return 结果
     */
    @Override
    public int updateMesInventory(MesInventory mesInventory) {
        return mesInventoryMapper.updateMesInventory(mesInventory);
    }

    /**
     * 批量删除库存信息
     * 
     * @param inventoryIds 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int deleteMesInventoryByInventoryIds(Long[] inventoryIds) {
        return mesInventoryMapper.deleteMesInventoryByInventoryIds(inventoryIds);
    }

    /**
     * 删除库存信息信息
     * 
     * @param inventoryId 库存信息主键
     * @return 结果
     */
    @Override
    public int deleteMesInventoryByInventoryId(Long inventoryId) {
        return mesInventoryMapper.deleteMesInventoryByInventoryId(inventoryId);
    }
}